<?php
namespace app\banquet\model;

use app\BaseModel;
use app\card\model\Company;
use app\member\model\Member;
use app\member\model\StoredOrder;
use think\facade\Db;

class Refund extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_banquet_refund';

    protected $append = [

        'company_name',

        'table_text'

    ];


    /**
     * @author chenniang
     * @DataTime: 2022-01-15 16:13
     * @功能说明:桌数拼接
     */
    public function getTableTextAttr($value,$data){

        if(isset($data['min_num'])&&isset($data['max_num'])){

            return $data['min_num'].'~'.$data['max_num'].'桌';

        }


    }
    /**
     * @author chenniang
     * @DataTime: 2022-01-14 14:19
     * @功能说明:
     */
    public function getImgsAttr($value,$data){

        if(!empty($value)){

            return explode(',',$value);
        }

    }


    /**
     * @author chenniang
     * @DataTime: 2022-01-07 17:46
     * @功能说明:查询公司名字
     */
    public function getCompanyNameAttr($value,$data){

        if(!empty($data['company_id'])){

            $company_model = new Company();

            $name = $company_model->where(['id'=>$data['company_id']])->value('name');

            return $name;

        }

    }
    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:00
     * @功能说明:添加
     */
    public function dataAdd($data){

        $data['create_time'] = time();

        $res = $this->insert($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:03
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:04
     * @功能说明:详情
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:05
     * @功能说明:
     */
    public function dataList($dis,$page=10){

        $data = $this->where($dis)->order('id desc')->paginate($page)->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2022-01-07 17:57
     * @功能说明:后端退款订单列表
     */
    public function orderRefundList($dis,$page=10){

        $data = $this->alias('a')
                ->join('longbing_card_banquet_order b','a.order_id = b.id','left')
                ->where($dis)
                ->field('a.*,b.order_code as pay_order_code')
                ->group('a.id')
                ->order('a.id desc')
                ->paginate($page)
                ->toArray();

        return $data;
    }




    /**
     * @author chenniang
     * @DataTime: 2021-03-18 09:37
     * @功能说明:通过退款
     */
    public function passOrder($id,$price,$payConfig,$refund_user=0,$text=''){

        $refund_order= $this->dataInfo(['id'=>$id]);

        $order_model = new Order();

        $pay_order   = $order_model->dataInfo(['id'=>$refund_order['order_id']]);

        if($refund_order['status']!=1){

            return ['code'=>500,'msg'=>'订单状态错误'];
        }

        $update = [

            'refund_user' => $refund_user,

            'status'      => 2,

            'refund_time' => time(),

            'refund_price'=> $price,

            'refund_text' => $text
        ];

        Db::startTrans();

        $res = $this->dataUpdate(['id'=>$refund_order['id']],$update);

        if($res!=1){

            Db::rollback();

            return ['code'=>500,'msg'=>'退款失败，请重试'];

        }

        $res = $order_model->dataUpdate(['id'=>$refund_order['order_id']],['pay_type'=>-1]);

        if($res!=1){

            Db::rollback();

            return ['code'=>500,'msg'=>'退款失败，请重试'];

        }

        $res = $this->refundCash($payConfig,$pay_order,$price,$refund_order);

        if(!empty($res['code'])){

            Db::rollback();

            return ['code'=>500,'msg'=>$res['msg']];
        }

        if($res!=true){

            Db::rollback();

            return ['code'=>500,'msg'=>'退款失败，请重试3'];
        }


        Db::commit();

        return true;

    }

    /**
     * @param $payConfig
     * @param $pay_order
     * @param $price
     * @param int $refund_id
     * @功能说明:退钱
     * @author chenniang
     * @DataTime: 2021-07-12 20:31
     */
    public function refundCash($payConfig,$pay_order,$price,$refund_order){

        if($price<=0){

            return true;
        }

        if($refund_order['balance']>0){

            $order_model = new Order();

            $refund_order['balance'] = $price;

            $order_model->orderStoredResult($refund_order,10);

        }else{
            //微信退款
            $response = orderRefundApi($payConfig,$pay_order['pay_price'],$price,$pay_order['transaction_id']);
            //如果退款成功修改一下状态
            if ( isset( $response[ 'return_code' ] ) && isset( $response[ 'result_code' ] ) && $response[ 'return_code' ] == 'SUCCESS' && $response[ 'result_code' ] == 'SUCCESS' ) {

                $response['out_refund_no'] = !empty($response['out_refund_no'])?$response['out_refund_no']:$pay_order['order_code'];

                $this->dataUpdate(['id'=>$refund_order['id']],['out_refund_no'=>$response['out_refund_no']]);

            }else {
                //失败就报错
                $discption = !empty($response['err_code_des'])?$response['err_code_des']:$response['return_msg'];

                return ['code'=>500,'msg'=> $discption];

            }

        }

        return true;


    }



    /**
     * @author chenniang
     * @DataTime: 2021-04-12 12:04
     * @功能说明:拒绝退款
     */
    public function noPassRefund($refund_id){

        $dis = [

            'id' => $refund_id
        ];

        $refund_order = $this->dataInfo($dis);

        if($refund_order['status']!=1){

            return ['code'=>500,'msg'=>'退款状态错误'];

        }

        $update = [

            'status'      => 3,

            'refund_time' => time()

        ];

        Db::startTrans();

        $res = $this->dataUpdate($dis,$update);

        if($res!=1){

            Db::rollback();

            return ['code'=>500,'msg'=>'退款失败，请重试1'];

        }
        Db::commit();

        return true;

    }






}